
$(function (){

    ajaxtree();

})

//树
function ajaxtree(){
    //ajax 请求数据
    $.ajax({
        type:'get',
        url:'/Cate/advanced/getpid',
        data:{},
        dataType:'json',
        success:function (res){
            treerender(res);
        }
    })

}


var name;
var cid;
function treerender(data){
    layui.use(['tree','table','layer','upload','form'],function () {
        var tree = layui.tree,
            layer = layui.layer,
            upload = layui.upload,
            form = layui.form,
            table = layui.table;


        var uploadInst = upload.render({
            elem: '#test1'
            , url: '/tp/upload' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                console.log(res.src);
                $("#test1").attr("lay-url",res.src)
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                //上传成功的一些操作
                //……
                //……
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传成功</span>');
                var fileUpdate = $('.image');
                fileUpdate.attr("value",res.src);//
                console.log("新传的图片的value值"+fileUpdate.attr("value"));
                $('#demoText').html(''); //置空上传失败的状态
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });




        tree.render({
            elem: '#ptree',
            data: data,
            id: 'ptreeID',
            showCheckbox: true,
            oncheck: function(obj){
                console.log(obj.data.id); //得到当前点击的节点数据
                name = obj.data.title;
                cid = obj.data.id;
            },
            click: function (obj) {
                console.log(obj.data);//得到当前点击的节点数据  id title , children
                // 将节点的id  存入 表单
                $("input[name='categroyId']").val(obj.data.id);

            },

        })


        //表的添加
        //监听提交按钮
        form.on('submit(addNodeform)',function (data){
            console.log(data.field)
            let attr = $("#test1").attr("lay-url");
            var s = {
                mainImage:attr
            }
            $.ajax({
                type:'post',
                url: '/JsProduct/advanced/addrizhi',
                data:{operatelist:'商品列表',operatetype:'添加',operateremark:'添加商品信息'},
                success:function(res){
                    if(top.window['refresh']){
                        top.window['refresh'].frames.location.reload();
                    }
                }
            })
            $.ajax({
                type:'post',
                url:'/JsProduct/advanced/add',
                data:data.field,
                dataType:'json',
                success:function (res){
                    console.log(res.data.id);
                    layer.msg(res.message,{time:500,icon:6})
                    //关闭所有弹框
                    parent.layer.closeAll();
                    //刷新树
                    parent.ajaxtree();
                    parent.ajaxtable();

                }
            })

            return false;
        })


        //监听树的监听按钮
        $('#queding').on('click',function (){
            console.log(cid);
            $("#title").val(name);
            $("#title1").val(cid);
            layer.close(index);
            $('#tree').css('display', 'none');
        })



    })

}
//小树的确定按钮
function queding() {
    layui.use('layer', function () {
        var layer = layui.layer;
    })
}

var index = null;

function tree() {
    layui.use('layer', function () {
        var layer = layui.layer;

        index = layer.open({
            type: 1,
            title: '添加',
            content: $("#tree"),
            area: ['65%', '100%'],
            success: function () {
                $('#tree').css('display', 'inherit');
            }
        })
    })
}
